<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Session;

/**
 * 会员接口
 */
class Adminapi extends Api
{
    protected $noNeedLogin = ['test','login','roomList','reNum','addRoom','editRoom','roomListauditing','roomdetail','orderList','orderdetail','MsgNumber','msglist','msgdetail','delmsg','sendmsg','getCreditor','cashlist','casholdlist','cashdetail','applycash','doapplycash'];
    protected $noNeedRight = '*';
    protected $hid = null;
    protected $adminId = null;

    public function _initialize()
    {
        parent::_initialize();

    }


    public function test(){
        echo 3;die;
    }

    //财务管理接口
    //获取财务页面数据接口
    public function getCreditor(){
        $hid = $this->request->request('hid');
        $uid = $this->request->request('uid');


        $starttime = $this->request->request('startTime');
        $endtime = $this->request->request('endTime');

        $todayS=strtotime($starttime); //int
        $todayE= strtotime($endtime);//int

        $oderdb = db("order");
        $oadb = db("cash");//订单申请提现表
        $res = [];
        $doingcashids = ""; //申请中
        $doingids = "";
        $docashids = "";//已处理
        $doids = "";
        $doingcash = $docash =  0;//申请中 收入金额


        if($todayS && !$todayE){
            //总收入【房费、基础服务费、退款费用】
            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->field("id")->select();
            //总收入
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("income");
            //总金额
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("all_money");
            //总房费
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("all_price");
            //总基础服务费bprice
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("ballprice");
            //总押金
            // $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayS")->where(["pay_status"=>3])->sum("deposit");

            //提现业务

            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime >= $todayS")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime >= $todayS")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";

                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }

        }
        if(!$todayS && $todayE){

            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("income");
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time <= $todayE")->where(["pay_status"=>3])->sum("ballprice");


            // $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time >= $todayE")->where(["pay_status"=>3])->sum("deposit");
            //提现业务
            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime <= $todayE")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime <= $todayE")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";

                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }

        }
        if($todayS && $todayE){
            $idslist = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("income");
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("ballprice");
            //  $deposit = $oderdb->where("hotelid in ($hid)")->where("add_time>='$todayS' and add_time <= '$todayE'")->where(["pay_status"=>3])->sum("deposit");
            //提现业务
            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->where("createtime>='$todayS' and createtime <= '$todayE'")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->where("createtime>='$todayS' and createtime <= '$todayE'")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";
                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }


        }
        if(!$todayS && !$todayE){

            //总收入【房费、基础服务费、退款费用】
            $idslist = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->field("id")->select();
            $income = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("income");
            //总金额
            $totalmoney = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("all_money");
            $allprice = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("all_price");
            $bprice = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("ballprice");
            //  $deposit = $oderdb->where("hotelid in ($hid)")->where(["pay_status"=>3])->sum("deposit");

            //提现业务

            //申请中数据计算
            $alist = $oadb->where("hotelid in ($hid)")->where("status=1")->field("id,oids")->select();
            if($alist){
                foreach ($alist as $v){
                    $doingcashids .= $v["oids"].",";
                    $doingids .= $v["id"].",";
                }
            }
            if($doingcashids != ""){
                $doingcashids  = rtrim($doingcashids,',');
                $doingids  = rtrim($doingids,',');
                $doingcash = $oderdb->where("id in ($doingcashids)")->where(["pay_status"=>3])->sum("income");
            }

            //已处理
            $alist2 = $oadb->where("hotelid in ($hid)")->where("status=2")->field("id,oids")->select();
            if($alist2){
                foreach ($alist2 as $vv){
                    $docashids .= $vv["oids"].",";
                    $doids .= $vv["id"].",";
                }
            }
            if($docashids != ""){
                $docashids  = rtrim($docashids,',');
                $doids  = rtrim($doids,',');
                $docash = $oderdb->where("id in ($docashids)")->where(["pay_status"=>3])->sum("income");
            }


        }

        if($idslist){
            $res['income']['ids'] = arr2str($idslist);
        }else{
            $res['income']['ids'] = "";
        }

        //申请中
        $res['cash']['doingcashids'] = $doingcashids;
        $res['cash']['doingappid'] = $doingcashids;
        $res['cash']['doingcash'] = $doingcash;
        //已处理
        $res['cash']['docashids'] = $docashids;
        $res['cash']['docash'] = $docash;



        $res['income']['income'] = $income;//总收入
        $res['income']['allmoney'] = $totalmoney;//总金额
        $res['income']['allprice'] = $allprice;//总房费
        $res['income']['bprice'] = $bprice;//总基础服务费

        $this->success('获取成功',$res);


    }

    //申请中、已处理等记录明细
    public function cashlist()
    {

            $id = $this->request->request("id");
            $list = db("order")->where("id in ($id)")->select();

            foreach ($list as &$row) {
                $room = db("house")->where(['id'=>$row['room_id']])->field("h_img")->find();
                $row['roomimg'] = "https://www.yunzhuke.net".$room['h_img'];

                //获取申请时间 审批时间
                $oid = $row['id'];
                $appinfo = db("cash")->where("find_in_set($oid,`oids`)")->find();
                $row['appcreatetime'] = date("Y-m-d H:i:s",$appinfo['createtime']);
                $row['appupdtime'] = date("Y-m-d H:i:s",$appinfo['updtime']);
                $row['appstatus'] = $appinfo['status'];
            }
            $this->success("获取记录成功",$list);

    }

    //处理提现订单
    public function doapplycash(){
        $ids = $this->request->request("oid");
        $uid = $this->request->request("uid");
        $olist = db("order")->where("id in ($ids)")->field("id,income,hotelid")->where("pay_status=3")->select();

        $insertdata = [];

        //订单根据酒店分组
        $indata = [];
        foreach ($olist as $v){
            if(array_key_exists($v['hotelid'],$indata)){
                $indata[$v['hotelid']]['oids'] .= ",".$v['id'];
                $indata[$v['hotelid']]['money'] += $v['income'];
            }else{
                $v['oids'] = $v['id'];
                $indata[$v['hotelid']] = $v;
                $indata[$v['hotelid']]['money'] = $v['income'];
            }

        }

        $now = time();
        foreach ($indata as $kk=>$vv ){
            unset($vv['id']);
            unset($vv['income']);
            $vv['adminid'] = $uid;
            $vv['status'] = 1;
            $vv['payment'] = 0;
            $vv['createtime'] = $now;
            array_push($insertdata,$vv);
        }

        $iaid = db("cash")->insertAll($insertdata);
        if($iaid){
            $this->success('申请成功，请等待审核');
        }else{
            $this->error("申请失败");
        }

    }

    //申请提现接口
    //处理逻辑，在订单表中找到当前酒店管理员所有的已完成的订单，不区分酒店管理员管理多个酒店的情况，后期可以再根据酒店分组，这里暂时不考虑
    //然后在过滤掉以及出现在cash表订单id的数据，因为一旦出现在cash，则代表被申请了，可能还为处理
    public function applycash(){
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error("参数错误");
        }
        $appids = "";
        $olist = [];

        $list = db("cash")->where("hotelid in ($hid)")->field("id,oids")->select();

        if($list){
            foreach ($list as $v){
                $appids .= $v["oids"].",";
            }
        }

        if($appids) {
            $appids = rtrim($appids, ',');
            //获取酒店未申请提醒的订单
            $olist = db("order")->where("hotelid in ($hid)")->where("id not in ($appids)")->where("pay_status = 3")->field("id,order_number,income")->select();
        }else{
            $olist = db("order")->where("hotelid in ($hid)")->where("pay_status = 3")->field("id,order_number,income")->select();

        }


        if($olist){
            foreach ($olist as &$v){
                $v['class'] = "yesselect";
                $v['income'] = (float)$v['income'];
            }
        }
        $this->success("获取成功",$olist);
    }

    //申请提现记录

    public function casholdlist()
    {

            $hid = $this->request->request("hid");
            if(empty($hid)){
                $this->error("参数错误");
            }

            $list = db("cash")->where("hotelid in ($hid)")->order("payment desc,status desc")->select();
            foreach ($list as &$row) {
                //酒店名称
                $hotel = db("hotels")->where(['hid' => $row['hotelid']])->field("hoteltitle")->find();
                $row['htitle'] = $hotel['hoteltitle'];
                $row['money'] = 100;
                $row['appcreatetime'] = date("Y-m-d H:i:s", $row['createtime']);
                if ($row['updtime']) {
                    $row['appupdtime'] = date("Y-m-d H:i:s", $row['updtime']);
                }
            }

            $this->success("获取成功",$list);


    }

    //提现申请详情
    public function cashdetail(){
        $id = $this->request->request("id");
        if(empty($id)){
            $this->error("参数错误");
        }

        $row = db("cash")->where("id=".$id)->find();
        $row['appcreatetime'] = date("Y-m-d H:i:s", $row['createtime']);
        if($row['status'] == 1){
            $row['statustext'] = "待处理";
        }else{
            $row['statustext'] = "已处理";
        }
        if($row['payment'] == 1){
            $row['payment'] = "已打款";
        }else{
            $row['payment'] = "未打款";
        }
        if ($row['updtime']) {
            $row['appupdtime'] = date("Y-m-d H:i:s", $row['updtime']);
        }
        $hotel = db("hotels")->where(['hid' => $row['hotelid']])->field("hoteltitle")->find();
        $row['htitle'] = $hotel['hoteltitle'];

        $this->success("获取成功",$row);

    }


    //房费到期短信提醒
    public function sendmsg(){
        $id = $this->request->request("id");
        $uid = $this->request->request("uid");
        $info = db("order_msg")->where("id=$id")->find();
        if($info){
            $res = sendHotel($info["userphone"]);
            if($res == 1){
                $data["uid"] = $uid;
                $data["mid"] = $info['id'];
                $data["create_time"] = date("Y-m-d H:i:s",time());
                $data["content"] = "您的房间已到期，请及时退房";
                $inid = db("order_msg_list")->insertGetId($data);
                if($inid){
                    $this->success('短信发送成功',$data);
                }else{
                    $this->error('短信发送成功，信息写入失败');
                }

            }else{
                $this->error('短信发送失败');
            }
        }else{
            $this->error('数据有误');
        }
    }

    public function delmsg(){
        $id = $this->request->request("id");
        $info = db("order_msg")->where("id=$id")->find();
        if($info){
            $updata["del"] = 1;
            $updata["updatetime"] = date("Y-m-d H:i:s",time());
            db("order_msg")->where(['id'=>$id])->update($updata);
            if($info['type'] == 4){
                $msglist = db("order_msg_list")->where(['mid'=>$id])->select();
                if($msglist){
                    db("order_msg_list")->where(['mid'=>$id])->update($updata);
                }

            }
            $this->success('删除成功');

        }else{
            $this->error('数据有误');
        }



    }
    public function msgdetail(){
        //当点开页面后就代表已读了消息

        $id = $this->request->request("id");

        $info = db("order_msg")->where("id=$id")->find();
        if($info){
            //修改为已读
            db("order_msg")->where("id=$id")->update(['status'=>1]);
            $order = db("order")->where(["id"=>$info['oid']])->field("id,order_name,order_phone,order_number,pay_status,pay_time,totalprice,checkout_time,moveinto_time,add_time")->find();
            $order['add_time'] = date("Y-m-d H:i:s",$order['add_time']);
            $order['moveinto_time'] = date("Y-m-d H:i:s",$order['moveinto_time']);
            $order['checkout_time'] = date("Y-m-d H:i:s",$order['checkout_time']);
            $info["order"] = $order;
            if($info['type'] == 4){
                $info['msglist'] = db("order_msg_list")->where(['mid'=>$info['id'],"del"=>0])->select();
            }else{
                $info['msglist'] = [];
            }

            $this->success('获取成功',$info);
        }else{
            $this->error('暂无数据');
        }


    }
    public function msglist(){
        $uid = $this->request->request("uid");
        if(empty($uid)){
            $this->error('账号过期，请重新登陆','',9);
        }
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error('酒店账号数据有误');
        }
        $status = $this->request->request("status");
        $type = $this->request->request("type");
        if($type == 0){
            $list = db("order_msg")->where("del=0 and status=$status and hid in ($hid) and uid = $uid")->select();
        }else{
            $list = db("order_msg")->where("del=0 and type=$type and status=$status and hid in ($hid) and uid = $uid")->select();
        }

        if($list){
            $this->success('获取成功',$list);
        }else{
            $this->error('暂无数据');
        }





    }

    //订单未读数量 消息通知，包含，已预定、已入住、已退房、房费到期提醒（每次提醒需要追加到日志，查看提醒次数）
    public function MsgNumber(){
        $uid = $this->request->request("uid");
        if(empty($uid)){
            $this->error('账号过期，请重新登陆','',9);
        }
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error('酒店账号数据有误');
        }
        $count = db("order_msg")->where("del=0 and status = 0 and hid in ($hid) and uid = $uid")->count();
        $this->success('获取成功',$count);

    }

    //订单详情
    public function orderdetail(){
        $id = $this->request->request("id");
        $info = db("order")->where(['id'=>$id])->find();
        if($info){

            $roominfo = json_decode($info['room_html'],true);
            $info['roomtitle'] = $roominfo['h_title'];
            $hinfo = db("hotels")->where(['hid'=>$roominfo['h_id']])->field("hoteltitle")->find();
            $info['hoteltitle'] = $hinfo['hoteltitle'];
            $info['add_time'] = date("Y-m-d H:i:s",$info['add_time']);
            $info['pay_time'] = date("Y-m-d H:i:s",$info['pay_time']);
            $info['moveinto_time'] = date("Y-m-d H:i:s",$info['moveinto_time']);
            $info['checkout_time'] = date("Y-m-d H:i:s",$info['checkout_time']);
            if($info['pay_type'] == 1){
                $info['pay_type_title'] = "余额";
            }elseif($info['pay_type'] == 2){
                $info['pay_type_title'] = "小时卡";
            }else{
                $info['pay_type_title'] = "微信";
            }
            if($info['pay_status'] == 1){
                $info['pay_status'] = "已预定";
            }elseif($info['pay_status'] == 2){
                $info['pay_status'] = "已入住";
            }elseif($info['pay_status'] == 3){
                $info['pay_status'] = "已完成";
            }elseif($info['pay_status'] == 4){
                $info['pay_status'] = "已取消";
            }elseif($info['pay_status'] == 5){
                $info['pay_status'] = "支付失败";
            }else{
                $info['pay_status'] = "未支付";
            }

            if($info['refund_status'] == 1){
                $info['refund_status'] = "已抵消";
            }elseif($info['refund_status'] == 2){
                $info['refund_status'] = "申请退款";
            }elseif($info['refund_status'] == 3){
                $info['refund_status'] = "已退款";
            }elseif($info['refund_status'] == 4){
                $info['refund_status'] = "退款失败";
            }else{
                $info['refund_status'] = "未执行退款";
            }


            $this->success('获取成功',$info);
        }else{
            $this->error('暂无数据');
        }

    }
    //订单列表
    public function orderList(){
        $uid = $this->request->request("uid");
        if(empty($uid)){
            $this->error('账号过期，请重新登陆','',9);
        }
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error('酒店账号数据有误');
        }
        $status = $this->request->request("status");
        $filename = $this->request->request("fileName");

        if(empty($filename)){
            $list = db("order")->where("pay_status = $status and hotelid in ($hid)")->field("id,order_number,pay_status,hotelid,order_name,order_phone")->order("id desc")->select();
        }else{
            $list = db("order")->where("order_name like '%$filename%' || order_phone like '%$filename%'")->where("pay_status = $status and hotelid in ($hid)")->field("id,order_number,pay_status,hotelid,order_name,order_phone")->order("id desc")->select();
        }

        if($list){
            $this->success('获取成功',$list);
        }else{
            $this->error('暂无数据');
        }
    }
    //后台登陆接口
    public function login(){
        $username = $this->request->request("userName");
        $password = $this->request->request("password");

        $admin = db("admin")->where(['username'=>$username])->find();

        if($admin){

            if ($admin['status'] != 'normal') {
                $this->error('账号被禁用，请联系管理员');
            }

            if ($admin['password'] != md5(md5($password) . $admin['salt'])) {
                $this->error('密码错误，请核实密码');
            }

            //超级管理员获取所有酒店列表
            if($admin['hotelid'] == 0){
                echo 2;die;
            }else{
                $hotelid = $admin['hotelid'];
                $hotels = db("hotels")->where("h_status = 'normal' and hid in ($hotelid)")->field('hid,hoteltitle')->select();
                $admin['hotels'] = $hotels;
            }



            $this->success('登陆成功',$admin);


        }else{
            $this->error('账号不存在，请核实账号');
        }

    }
    //获取酒店详情
    public function roomdetail(){
        $id = $this->request->request("id");
        $info = db("house")->where(['id'=>$id])->find();
        if($info){
            $info['unitlist'] = $this->reNumsengle($info['h_id']);
            $this->success('获取成功',$info);
        }else{
            $this->error('暂无数据');
        }

    }
    //发布房源
    public function editRoom(){
        $id = $this->request->request("roomid");
        $title = $this->request->request("title");
        $attr = $this->request->request("attr");
        $housebrand = $this->request->request("housebrand");
        $imgs = $this->request->request("imgs");
        $img = $this->request->request("img");
        $hotelid = $this->request->request("hotelid");
        $rstorey = $this->request->request("rstorey");
        $price = $this->request->request("price");
        $weekprice = $this->request->request("weekprice");
        $times = $this->request->request("times");
        $hpaysytle = $this->request->request("hpaysytle");
        $hservs = $this->request->request("hservs");
        $hservice = $this->request->request("hservice");
        $hservice2 = $this->request->request("hservice2");
        $hread = $this->request->request("hread");
        $hcontent = $this->request->request("hcontent");
        $hpaymentroom = $this->request->request("hpaymentroom");
        $hserviceroom = $this->request->request("hserviceroom");
        $status = $this->request->request("status");
        $statusText = $this->request->request("statusText");
        $auditing = $this->request->request("auditing");


        //由于前端做过校验 ，这里就不做合法校验


        $data['h_id'] = $hotelid;
        $data['h_housebrand'] = $housebrand;
        $data['h_storey'] = $rstorey;
        $data['h_title'] = $title;
        $data['h_read'] = $hread;
        $data['h_servs'] = $hservs;
        $data['h_service'] = $hservice;
        $data['h_content'] = $hcontent;
        $data['h_statusText'] = "normal";
        $data['updatetime'] = time();
        $data['h_imgs'] = $imgs;
        $data['h_img'] = $img;
        $data['h_price'] = $price;
        $data['h_weekprice'] = $weekprice;
        $data['h_times'] = $times;
        $data['h_depositroom'] = $price * $times;
        $data['h_allprice'] = $price * $times;
        $data['h_attr'] = $attr;
        $data['h_status'] = $status;
        $data['h_statusText'] = $statusText;
        $data['auditing'] = $auditing;
        $data['h_service2'] = $hservice2;
        $data['h_paymentroom'] = $hpaymentroom;
        $data['h_serviceroom'] = $hserviceroom;
        $data['h_paysytle'] = $hpaysytle;

        //修改房源状态是需要判断 该 房间此时的状态，如果正在使用中，禁止修改，如果被预定而已不能修改，


        // 当房源 变成空闲、隐藏、维修、打扫 时 要先判断当前房源是否在订单里被占用
        if($statusText == 'hidden' || $statusText == "repair" || $statusText == "cleaning" || $statusText == "normal"){
            $order = db("order")->where(['room_id'=>$id])->where("pay_status in (1,2)")->find();
            if($order){
                $this->error('房源已经被预定或者入住，暂时不能修改房源状态');
            }
        }

        //如果房源变成入住预定 先要判断订单里是否有这个房间的订单如果没有，禁止预定
        if($statusText == "reserve"){
            $order = db("order")->where(['room_id'=>$id])->where("pay_status = 1")->find();
            if(!$order){
                $this->error('未找到预定该房源订单，请核实后修改');
            }
        }

        //如果房源变成入住预定 先要判断订单里是否有这个房间的订单如果没有，禁止预定
        if($statusText == "using"){
            $order = db("order")->where(['room_id'=>$id])->where("pay_status = 1")->order("id asc")->find();
            if(!$order){
                $this->error('未找到预定该房源订单，不能修改为使用状态');
            }else{
                //修改订单状态
                $odata['pay_status'] = 2;
                $odata['moveinto_time'] = time();
                $odata['upd_time'] = time();

               $eff = db("order")->where(['order_number'=>$order['order_number']])->update($odata);

               if(!$eff){
                   $this->error('订单入住失败');
               }
            }
        }

        $effect = db("house")->where(['id'=>$id])->update($data);
        if($effect){
            $this->success('房源更新成功');
        }else{
            $this->error('暂无数据');
        }
    }
    //发布房源
    public function addRoom(){
        $title = $this->request->request("title");
        $attr = $this->request->request("attr");
        $housebrand = $this->request->request("housebrand");
        $imgs = $this->request->request("imgs");
        $img = $this->request->request("img");
        $hotelid = $this->request->request("hotelid");
        $rstorey = $this->request->request("rstorey");
        $price = $this->request->request("price");
        $weekprice = $this->request->request("weekprice");
        $times = $this->request->request("times");
        $hpaysytle = $this->request->request("hpaysytle");
        $hservs = $this->request->request("hservs");
        $hservice = $this->request->request("hservice");
        $hservice2 = $this->request->request("hservice2");
        $hread = $this->request->request("hread");
        $hcontent = $this->request->request("hcontent");
        $hpaymentroom = $this->request->request("hpaymentroom");
        $hserviceroom = $this->request->request("hserviceroom");
        $status = $this->request->request("status");


        //由于前端做过校验 ，这里就不做合法校验


        $data['h_id'] = $hotelid;
        $data['h_housebrand'] = $housebrand;
        $data['h_storey'] = $rstorey;
        $data['h_title'] = $title;
        $data['h_read'] = $hread;
        $data['h_servs'] = $hservs;
        $data['h_service'] = $hservice;
        $data['h_content'] = $hcontent;
        $data['h_statusText'] = "normal";
        $data['createtime'] = time();
        $data['h_imgs'] = $imgs;
        $data['h_img'] = $img;
        $data['h_price'] = $price;
        $data['h_weekprice'] = $weekprice;
        $data['h_times'] = $times;
        $data['h_depositroom'] = $price * $times;
        $data['h_allprice'] = $price * $times;
        $data['h_attr'] = $attr;
        $data['h_status'] = $status;
        $data['h_service2'] = $hservice2;
        $data['h_paymentroom'] = $hpaymentroom;
        $data['h_serviceroom'] = $hserviceroom;
        $data['h_paysytle'] = $hpaysytle;

        $insertid = db("house")->insertGetId($data);
        if($insertid){
            $this->success('发布成功，请等待管理员审核');
        }else{
            $this->error('暂无数据');
        }
    }
    //获取登陆账号房源列表 已经审核的 auditing = 1
    public function roomList(){
        $uid = $this->request->request("uid");
        if(empty($uid)){
            $this->error('账号过期，请重新登陆','',9);
        }
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error('酒店账号数据有误');
        }
        $status = $this->request->request("status");
        //正常normal 使用中 using 维修 repair 打扫cleaning 预定 reserve 隐藏 hidden
        if($status == 1){
            $hstatus = "using";
        }elseif ($status == 2){
            $hstatus = "reserve";
        }elseif ($status == 3){
            $hstatus = "cleaning";
        }elseif ($status == 4){
            $hstatus = "repair";
        }elseif ($status == 5){
            $hstatus = "hidden";
        }else{
            $hstatus = "normal";
        }

        $rlist = db("house")->where("auditing = 1 and h_status = 'normal' and h_statusText = '{$hstatus}' and h_id in ($hid)")->field("id,h_id,h_title,h_img,h_statusText")->select();

        if($rlist){
            $this->success('获取成功',$rlist);
        }else{
            $this->error('暂无数据');
        }

    }
    //获取登陆账号房源列表 审核数据
    public function roomListauditing(){
        $uid = $this->request->request("uid");
        if(empty($uid)){
            $this->error('账号过期，请重新登陆','',9);
        }
        $hid = $this->request->request("hid");
        if(empty($hid)){
            $this->error('酒店账号数据有误');
        }
        $auditing = $this->request->request("auditing");

        $rlist = db("house")->where("h_status = 'normal' and auditing = '{$auditing}' and h_id in ($hid)")->field("id,h_id,h_title,h_img,auditing")->select();

        if($rlist){
            $this->success('获取成功',$rlist);
        }else{
            $this->error('暂无数据');
        }

    }
    //返回楼层号数组
    public function reNum(){
        $hid = $this->request->request("hid");
        $info = db("hotels")->where(['hid'=>$hid])->field("h_storey")->find();

        if($info){
            $index = array();
            $id = $info['h_storey'];
            if($id == 1){
                array_push($index,1);
            }else{
                for ($i = 1;$i<=$id;$i++){
                    array_push($index,$i);
                }
            }

            if($index){
                $this->success('获取成功',$index);
            }else{
                $this->error('暂无数据');
            }
        }else{
            $this->error('酒店数据有误');
        }


    }
    //返回楼层号数组 单个使用
    public function reNumsengle($hid){
        $info = db("hotels")->where(['hid'=>$hid])->field("h_storey")->find();

        if($info){
            $index = array();
            $id = $info['h_storey'];
            if($id == 1){
                array_push($index,1);
            }else{
                for ($i = 1;$i<=$id;$i++){
                    array_push($index,$i);
                }
            }

            if($index){
                 return $index;
            }else{
                $this->error('暂无数据');
            }
        }else{
            $this->error('酒店数据有误');
        }

    }














}
